    //
//  AppDelegate.swift
//  SinaWb
//
//  Created by Jiang on 16/5/7.
//  Copyright © 2016年 Jiang. All rights reserved.
//

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    weak  var obser : AnyObject?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        JkLog(path)
        Addoser()
        window = UIWindow(frame: UIScreen.mainScreen().bounds )
        window?.rootViewController = GetTheDefaultRootViewController()
        window?.makeKeyAndVisible()
        SetNaviAndTabBarCololr()
        return true
    }
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(obser!)
    }

}
extension AppDelegate{
    
    func Addoser(){
       obser =  NSNotificationCenter.defaultCenter().addObserverForName("JkmAppDelegate", object: nil, queue: nil) { (object) in
            
            if object.userInfo == nil{
                
                self.window?.rootViewController = JkmWelcomeViewController()
                return
            }
            self.window?.rootViewController = ViewModel.GetTheMainTabBarController()
        }
    }
    
    
    func GetTheDefaultRootViewController()-> UIViewController{
        if ViewModel.sharViewModel.userLogin {
            return haveNewVersion() ? JkmNewfeatureViewController() : JkmWelcomeViewController()
        }
        
        return ViewModel.GetTheMainTabBarController()
        
    }
    
    
    func haveNewVersion()->Bool{
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let versionKey = "SinWbVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(versionKey) ?? "0.0"
        
        if currentVersion.compare(sandboxVersion) == NSComparisonResult.OrderedDescending{
            
            print(sandboxVersion)
            
             NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: versionKey)
            
            return true
        }
        
        
        return false
    }
    
    func SetNaviAndTabBarCololr(){
        
        UINavigationBar.appearance().tintColor = ThemeColor
        UITabBar.appearance().tintColor = ThemeColor
    }
}

func JkLog<T>(meesage : T ,_ fileName :String = #file, _ Method :String = #function ,_ lineNum :Int = #line  ){
        #if DEBUG
            let file = (fileName as NSString ).pathComponents.last!
            let name = file.substringToIndex(file.characters.indexOf(".")!)
            print("\(name)->\(Method)[\(lineNum)]:  \(meesage)")
        #endif
}
